package com.kwamecorp.brunoperry.kwamelogo.engine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;

public class KC3DLoader
{
	protected Context context;
	protected KCListener listener;

	protected float[] vertices;
	protected float[] uvs;
	protected float[] normals;

	protected short[] vertexIndices;
	protected short[] uvIndices;
	protected short[] normalIndices;

	protected int numVerticesIndices;
	protected int numUVSIndices;
	protected int numNormalsIndices;

	private ArrayList<Float> verticesList;
	private ArrayList<Float> uvsList;
	private ArrayList<Float> normalsList;

	private ArrayList<Short> verticesIndexList;
	private ArrayList<Short> uvsIndexList;
	private ArrayList<Short> normalsIndexList;

	public KC3DLoader(Context context) {

		this.context = context;
		init();
	}

	private void init()
	{

		verticesList = new ArrayList<Float>();
		uvsList = new ArrayList<Float>();
		normalsList = new ArrayList<Float>();

		verticesIndexList = new ArrayList<Short>();
		uvsIndexList = new ArrayList<Short>();
		normalsIndexList = new ArrayList<Short>();
	}

	public void setOnCompleteListener(KCListener kcListner)
	{
		listener = kcListner;
	}

	public void loadOBJ(int resId)
	{
		InputStream inputStream = context.getResources().openRawResource(resId);
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		String line;
		try
		{
			line = reader.readLine();
			while (line != null)
			{
				if (line.contains("v "))
				{
					parseVertices(line);
				}
				if (line.contains("vt "))
				{
					parseUVS(line);
				}
				if (line.contains("vn "))
				{
					parseNormals(line);
				}
				if (line.contains("f "))
				{
					parseIndices(line);
				}
				line = reader.readLine();
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		vertices = setVertexData(verticesList);
		uvs = setVertexData(uvsList);
		normals = setVertexData(normalsList);

		vertexIndices = setIndexData(verticesIndexList);
		uvIndices = setIndexData(uvsIndexList);
		normalIndices = setIndexData(normalsIndexList);

		numVerticesIndices = vertexIndices.length;
		numUVSIndices = uvIndices.length;
		numNormalsIndices = normalIndices.length;

		fileLoaded();
	}

	private float[] setVertexData(ArrayList<Float> from)
	{
		float[] to = new float[from.size()];
		for (int i = 0; i < from.size(); i++)
		{
			to[i] = from.get(i);
		}

		return to;
	}

	private short[] setIndexData(ArrayList<Short> from)
	{

		short[] to = new short[from.size()];
		for (int i = 0; i < from.size(); i++)
		{
			to[i] = from.get(i);
		}
		return to;
	}

	private void parseIndices(String line)
	{
		String[] vertexComponents = line.split(" ");
		String[] va = vertexComponents[1].split("/");
		String[] vb = vertexComponents[2].split("/");
		String[] vc = vertexComponents[3].split("/");

		int val;

		val = Integer.valueOf(va[0]) - 1;
		verticesIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vb[0]) - 1;
		verticesIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vc[0]) - 1;
		verticesIndexList.add(Short.valueOf("" + val));

		val = Integer.valueOf(va[1]) - 1;
		uvsIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vb[1]) - 1;
		uvsIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vc[1]) - 1;
		uvsIndexList.add(Short.valueOf("" + val));

		val = Integer.valueOf(va[2]) - 1;
		normalsIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vb[2]) - 1;
		normalsIndexList.add(Short.valueOf("" + val));
		val = Integer.valueOf(vc[2]) - 1;
		normalsIndexList.add(Short.valueOf("" + val));
	}

	private void parseNormals(String line)
	{
		String[] normals = line.split(" ");
		normalsList.add(Float.valueOf(normals[1]));
		normalsList.add(Float.valueOf(normals[2]));
		normalsList.add(Float.valueOf(normals[3]));
	}

	private void parseUVS(String line)
	{
		String[] uvs = line.split(" ");
		uvsList.add(Float.valueOf(uvs[1]));
		uvsList.add(Float.valueOf(uvs[2]));
	}

	private void parseVertices(String line)
	{
		String[] verts = line.split(" ");
		verticesList.add(Float.valueOf(verts[1]));
		verticesList.add(Float.valueOf(verts[2]));
		verticesList.add(Float.valueOf(verts[3]));
	}

	protected void fileLoaded()
	{
		listener.onComplete();
	}

	protected float[] indexData(float[] data, short[] indexData)
	{
		float[] dataOut = new float[indexData.length];

		for (int i = 0; i < indexData.length; i++)
		{
			dataOut[i] = data[indexData[i]];
		}

		return dataOut;
	}
}
